﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace uTrade.Data
{
    internal class SelfSelected
    {
        public SelfSelected()
        {
        }

        private DataSet GetAllSelfSelected()
        {
            return SelfSelectedDAL.GetList("Symbol is not null");
        }

        private int AddSelfSelected(SelfSelectedModel model)
        {
            return SelfSelectedDAL.Add(model);
        }

        private int UpdateSelfSelected(SelfSelectedModel model)
        {
            return SelfSelectedDAL.Update(model);
        }

        //更新当前的盈利信息
        private void UpdateProfit()
        {
            DataTable dsSelecteTbl = GetAllSelfSelected().Tables[0];
            List<SelfSelectedModel> oLstModelSelf = ModelConvertHelper<SelfSelectedModel>.ConvertToModel(dsSelecteTbl).ToList();

            foreach (var oModelSelf in oLstModelSelf)
            {
                EquityModel buymodel = GetFundEquityInfo.GetFormatedFundInfo(oModelSelf.Symbol, oModelSelf.BuyDate);
                oModelSelf.BuyPrice = float.Parse(buymodel.unitwork);
                //买入份额
                double dBuyCount = oModelSelf.BuyQuant / oModelSelf.BuyPrice;
                if (oModelSelf.SaleDate == null)
                {
                    oModelSelf.SaleDate = DateTime.Today;
                }

                EquityModel salemodel = GetFundEquityInfo.GetFormatedFundInfo(oModelSelf.Symbol, oModelSelf.SaleDate);

                oModelSelf.SalePrice = float.Parse(salemodel.unitwork);
                oModelSelf.CurQuant = dBuyCount * oModelSelf.SalePrice;
                oModelSelf.CurProfit = oModelSelf.CurQuant - oModelSelf.BuyQuant;
                SelfSelectedDAL.Update(oModelSelf);
            }
        }
    }
}